package org.fly.api.demo.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Dict;
import org.fly.api.common.core.R;
import org.fly.api.common.core.constant.UserConstants;
import org.fly.api.common.core.domain.LoginUser;
import org.fly.api.common.core.enums.UserType;
import org.fly.api.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: SaTokenController
 * @Description: 测试 satoken 工具包
 * @Date: 2023/12/11 14:10
 * @author: Blue
 */
@RestController
@RequestMapping("/api/sa-token")
public class TestSaTokenController {


    /**
     *  会话登录接口
     * @param name 用户名
     * @param pwd 密码
     * @return {@link R}<{@link String}>
     */
    @GetMapping("/doLogin")
    @SaIgnore
    public R<Dict> doLogin(String name, String pwd) {
        // 第一步：比对前端提交的账号名称、密码
        if("zhang".equals(name) && "123456".equals(pwd)) {
            // 第二步：根据账号id，进行登录
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(1L);
            loginUser.setUsername("zhang");
            loginUser.setUserType(UserType.SYS_USER.getUserType());
            LoginHelper.login(loginUser);

            Dict dict = Dict.create();
            dict.set("accessToken", UserConstants.TOKEN_PREFIX + StpUtil.getTokenValue()).set("expireIn", StpUtil.getTokenTimeout());

            return R.ok(dict);
        }
        return R.fail("登录失败");
    }

    @GetMapping("/info")
    public R<String> info(){
        return R.ok("访问成功");
    }

}
